<?php
class VisitorsController extends AppController {

	var $name = 'Visitors';
    var $components = array('Json');
    var $helpers = array('Javascript');

    function beforeFilter(){
        parent::beforeFilter();
    }

    /**
     * Giving user comment permission by email
     * usage in /comments/manage.ctp
     */
    function approve(){
        $this->layout = 'ajax';
        Configure::write('debug',0);
        App::import('Sanitize');

        /* Defining tags array variables from ajax input */
        $email = Sanitize::escape($this->params['form']['email']);
        $checkEmail = $this->Visitor->checkEmail($email);
        if(empty($checkEmail)){
            $this->data['Visitor']['email']=$email;
            $this->data['Visitor']['status']='1';
            $this->Visitor->create();
            if($this->Visitor->save($this->data)){
                /* Updating visitor id on comment tables */
                $this->Visitor->Comment->updateVisitorId($email,$this->Visitor->id);
                
                /* Counting incoming comment by visitor id */
                $count = $this->Visitor->Comment->countCommentVisitor($this->Visitor->id);
                $this->data['Visitor']['comment_count']=$count;

                $this->Json->result('1');
            }
        }else{
            $this->Json->error('1');
        }        
    }

    /**
     * Giving user comment permission by email
     * usage in /comments/manage.ctp
     */
    function deny(){
        $this->layout = 'ajax';
        Configure::write('debug',0);
        App::import('Sanitize');

        /* Defining tags array variables from ajax input */
        $email = Sanitize::escape($this->params['form']['email']);
        $checkEmail = $this->Visitor->checkEmail($email);
        if(!empty($checkEmail)){
            $this->Visitor->del($checkEmail['Visitor']['visitor_id']);
            $this->Visitor->Comment->removeVisitorId($checkEmail['Visitor']['visitor_id']);
            $this->Json->result('1');
        }else{
            $this->Json->error('1');
        }
    }

    /**
     * Giving user comment permission by email
     * usage in /comments/manage.ctp
     */
    function spam(){
        $this->layout = 'ajax';
        Configure::write('debug',0);
        App::import('Sanitize');

        /* Defining tags array variables from ajax input */
        $email = Sanitize::escape($this->params['form']['email']);
        $checkEmail = $this->Visitor->checkEmail($email);
        if(!empty($checkEmail)){
            $this->data['Visitor']['email']=$email;
            $this->data['Visitor']['status']='0';
            $this->Visitor->create();
            if($this->Visitor->save($this->data)){
                /* Updating visitor id on comment tables */
                $this->Visitor->Comment->updateVisitorId($email,$this->Visitor->id);
                $this->Visitor->Comment->deleteCommentByVisitorId($this->Visitor->id);
                $this->Json->result('1');
            }
        }else{
            $this->Json->error('1');
        }
    }


}
?>
